package cn.edu.ccibe.exam16system.controller;

import cn.edu.ccibe.exam16system.entity.Student;
import cn.edu.ccibe.exam16system.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.websocket.server.PathParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class StudentController {

    @Resource
    private StudentService studentService;

    @GetMapping("/students")
    public String students(
            @PathParam("page") Integer page,
            @PathParam("pageSize") Integer pageSize,
            @PathParam("fragment") Integer fragment,
            @PathParam("keyword") String keyword,
            Model model) {
        if (page == null) page = 1;
        if (pageSize == null) pageSize = 10;
        if (fragment == null) fragment = 0;
        if (keyword == null) keyword = "";
        IPage<Student> studentPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Student::getName, keyword).or()
                .like(Student::getGender, keyword).or()
                .like(Student::getClassName, keyword);
        IPage<Student> pageResult = studentService.page(studentPage, wrapper);
        model.addAttribute("keyword", keyword);
        model.addAttribute("studentPage", pageResult);
        if (fragment == 1) return "students::content";
        return "students";
    }
}
